(function($){
    $.extend({
        "addJavascript":function(scriptUrl, reload){
            if(!reload){
                var regex = new RegExp(scriptUrl+"$");
                var isPresent = false;
                $("script").each(function(tagSr, scriptTag){
                    if(regex.test(scriptTag.src)){
                        isPresent = true;
                        return false;
                    }
                //console.log(scriptTag.src + "==" + scriptUrl +"--" + isPresent);
                });
                if(isPresent) return;
            } 
            var scriptTag = document.createElement("script");
            scriptTag.type = "text/javascript";
            scriptTag.src = scriptUrl;
            document.getElementsByTagName("head")[0].appendChild(scriptTag);
        },
        "addStylesheet":function(cssUrl, reload){
            
            if(!reload){
                var regex = new RegExp(cssUrl+"$");
                var isPresent = false;
                $("link").each(function(tagNo, styleTag){
                    if(regex.test(styleTag.href)){
                        isPresent = true;
                        return false;
                    }
                //console.log(styleTag.src + "==" + cssUrl +"--" + isPresent);
                });
                if(isPresent) return;
            }
            
            var styleTag = document.createElement("link");
            styleTag.type = "text/css";
            styleTag.rel = "stylesheet";
            styleTag.src = cssUrl;
            document.getElementsByTagName("head")[0].appendChild(styleTag);
        }
    });

    bela.prep["includeResource"] = function(key,value,jsonParent,jqueryParent,belaJson){
        if(key == "->res"){
            $.each(value,function(i,v){
                if(/.*\.js$/.test(v)){
                    $.addJavascript(v);
                }else if(/.*\.css$/.test(v)){
                    $.addStylesheet(v);
                } 

            });
        }else if(key == "->resReload"){
                if(/.*\.js$/.test(v)){
                    $.addJavascript(v,true);
                }else if(/.*\.css$/.test(v)){
                    $.addStylesheet(v,true);
                } 
            
        }
    };
    
}(jQuery));

